#!/bin/bash
#=======================================================================================
#  Name:  rc.autofan
#=======================================================================================
#  Description:
#
#  This script has the aim to control autofan script
#  How to invoke in your "go" script (copy to /boot/scripts):
#  chmod +x /boot/scripts/fan_speed.sh -- superseeded by Dynamix plugin
#  /boot/fan_speed.sh                  -- superseeded by Dynamix plugin
#=======================================================================================
# Version 1.0   Authored by Aiden
# Version 1.1   Modified by Dan Stroot to run in a loop. Does not require the user
#               to add this to cron     - just start it in your go file.
# Version 1.2   Modified by Guzzi       - removed -d ata to work on sas controllers
# Version 1.3   Modified by gfjardim    - added to dynamix.system.autofan
# Version 1.4   Modified by Bergware    - added additional line parameters
#                                       - start/stop control by Dynamix plugin
#                                       - celsius/fahrenheit conversion
# Version 1.5   Modified by Bergware    - added nvme devices temperature reading (contributed by syntactic-salt)
#                                       - added exclude disks from temperature reading (contributed by andreiio)
# Version 1.6   Modified by InfinityMod - added multifan support
#                                       - added fan spin up/down offset 
plugin=dynamix.system.autofan
script=autofan
execute=/usr/local/emhttp/plugins/$plugin/scripts/$script
cfg_path=/boot/config/plugins/$plugin

autofan.start() {
  for cfg in $cfg_path/$plugin*.cfg; do
    source $cfg
    if [[ $service == 1 ]]; then
      $execute $options 1>/dev/null 2>&1
      echo "$script started"
    fi
  done 
}

autofan.stop() {
  for cfg in $cfg_path/$plugin*.cfg; do
    source $cfg
    $execute -c $controller -f $fan -q 1>/dev/null 2>&1
    timer=5
    until [[ -z $(pgrep -f $execute) || $timer -eq 0 ]]; do
      timer=$((timer-1))
      sleep 1
    done
    # set fan to max
    echo 255 > $controller
    echo "$script stopped"
  done
}

autofan.speed() {
  for cfg in $cfg_path/$plugin*.cfg; do
    source $cfg
    [[ -f $controller && $(cat $fan) -gt 0 ]] &&  echo -n "$(($(($(cat $controller)*100))/255)) " || echo -n "0 "
  done
}

autofan.restart() {
  autofan.stop
  sleep 2
  autofan.start
}

case "$1" in
'start')
 autofan.start ;;
'stop')
 autofan.stop ;;
'speed')
 autofan.speed ;;
'restart')
 autofan.restart ;;
*)
 echo "Usage: $(basename $0) start|stop|restart|speed"
esac
